@use 'sass:map';

@use './shared' as *;
@use './design' as *;
@use './input.scss' as input;

$date-picker: () !default;
$date-picker: map.merge(
  (
    color-disabled: get-css-var('input-control-color-disabled'),
    color-error: get-css-var('color-error-base'),
    unit-color: get-css-var('content-color-base'),
    unit-color-hover: get-css-var('color-primary-base'),
    unit-color-focus: get-css-var('color-white'),
    unit-bg-color-focus: get-css-var('color-primary-opacity-1'),
    unit-bg-color-error: get-css-var('color-error-opacity-1'),
    separator-color: get-css-var('content-color-disabled'),
    exchange-color: get-css-var('content-color-third'),
    panel-bg-color: get-css-var('bg-color-base'),
    panel-border-radius: get-css-var('radius-base'),
    panel-shadow: get-css-var('shadow-base'),
    shortcut-bg-color: get-css-var('fill-color-background'),
    shortcut-bg-color-hover: get-css-var('fill-color-hover'),
    d-color: get-css-var('border-color-light-2'),
    arrow-color: get-css-var('content-color-third'),
    arrow-color-hover: get-css-var('color-primary-base'),
    header-color-hover: get-css-var('color-primary-base'),
    header-height: 36px,
    item-color-hover: get-css-var('calendar-index-color-hover'),
    item-color-selected: get-css-var('calendar-index-color-selected'),
    item-color-today: get-css-var('calendar-index-color-today'),
    item-color-outside: get-css-var('calendar-index-color-outside'),
    item-color-disabled: get-css-var('calendar-index-color-disabled'),
    item-bg-color: get-css-var('calendar-index-bg-color'),
    item-bg-color-hover: get-css-var('calendar-index-bg-color-hover'),
    item-bg-color-selected: get-css-var('calendar-index-bg-color-selected'),
    item-bg-color-disabled: get-css-var('calendar-index-bg-color-disabled'),
    item-bg-color-today: get-css-var('calendar-index-bg-color-today'),
    item-bg-color-outside: get-css-var('calendar-index-bg-color-outside'),
    item-radius: get-css-var('calendar-index-radius'),
    range-bg-color: get-css-var('calendar-range-bg-color'),
    range-opacity: get-css-var('calendar-range-opacity')
  ),
  $date-picker
);

.#{$namespace}-date-picker {
  &-vars {
    @include define-preset-values('date-picker', $date-picker);
  }

  @include basis;

  display: inline-flex;
  width: 100%;

  @include input.handler(#{&}__selector);

  $wheel: '#{$namespace}-wheel';

  user-select: none;

  &__selector {
    cursor: pointer;
  }

  &--disabled &__selector {
    cursor: not-allowed;
  }

  &__control {
    position: relative;
    display: flex;
    align-items: center;
  }

  &__input {
    display: flex;
    align-items: center;
    outline: 0;

    &--error {
      color: get-css-var('date-picker-color-error');
    }
  }

  &__placeholder {
    overflow: hidden;
    color: get-css-var('input-placeholder-color');
    text-overflow: ellipsis;
    white-space: nowrap;
    pointer-events: none;
    user-select: none;
  }

  &__unit {
    padding: 0 2px;
    color: get-css-var('date-picker-unit-color');
    text-align: center;
    transition: get-css-var('transition-background'), get-css-var('transition-color');

    &:hover {
      color: get-css-var('date-picker-unit-color-hover');
    }

    &--focused,
    &--focused:hover {
      color: get-css-var('date-picker-unit-color-focus');
      background-color: get-css-var('date-picker-unit-bg-color-focus');
    }
  }

  &__input--error &__unit {
    color: get-css-var('date-picker-color-error');

    &--focused {
      color: get-css-var('date-picker-unit-color-focus');
      background-color: get-css-var('date-picker-unit-bg-color-error');
    }
  }

  &__label {
    text-align: center;
    user-select: none;
    transition: get-css-var('transition-background'), get-css-var('transition-color');
  }

  &__separator {
    line-height: 1;
    color: get-css-var('date-picker-separator-color');
    transition: get-css-var('transition-color');
  }

  &__input--error &__separator {
    color: get-css-var('date-picker-color-error');
  }

  &--disabled &__separator {
    color: get-css-var('date-picker-color-disabled');
  }

  &__exchange {
    display: flex;
    align-items: center;
    padding: 0 4px;
    color: get-css-var('date-picker-exchange-color');
  }

  &--disabled &__exchange {
    color: get-css-var('date-picker-color-disabled');
  }

  &__pad {
    width: 2px;
  }

  &__clear {
    @include clean-button;
  }

  &__popper {
    padding: 8px 0;
  }

  &__panel {
    display: flex;
    background-color: get-css-var('date-picker-panel-bg-color');
    border-radius: get-css-var('date-picker-panel-border-radius');
    box-shadow: get-css-var('date-picker-panel-shadow');

    &--vertical {
      flex-direction: column;
    }

    &-body {
      display: flex;
    }
  }

  &__list {
    display: flex;
    flex-direction: column;
  }

  &__shortcuts {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding: 6px 0;
    background-color: get-css-var('date-picker-shortcut-bg-color');
    border-inline-end: get-css-var('border-shape') get-css-var('date-picker-d-color');

    &--top,
    &--bottom {
      flex-flow: row wrap;
      justify-content: center;
      padding: 0 6px;
    }

    &--bottom,
    &--right {
      order: 1;
    }
  }

  &__shortcut {
    width: 100%;
    padding: 6px 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
    transition: get-css-var('transition-background');

    &:hover {
      background-color: get-css-var('date-picker-shortcut-bg-color-hover');
    }
  }

  &__shortcuts--top &__shortcut,
  &__shortcuts--bottom &__shortcut {
    width: auto;
  }

  &__header {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: get-css-var('date-picker-header-height');
    padding: 0 16px;
    border-bottom: get-css-var('border-shape') get-css-var('date-picker-d-color');

    &--time {
      padding: 0;
    }
  }

  &__title {
    display: flex;
    flex: 1;
    justify-content: center;
  }

  &__year-month {
    display: flex;
    flex: auto;
    justify-content: center;
    min-width: 0;
  }

  &__arrow {
    display: flex;
    justify-content: center;
    width: 14px;
    color: get-css-var('date-picker-arrow-color');
    cursor: pointer;
    transition: get-css-var('transition-color');

    &:hover {
      color: get-css-var('date-picker-arrow-color-hover');
    }
  }

  &__prev-year {
    margin-inline-end: 4px;
  }

  &__next-year {
    margin-inline-start: 4px;
  }

  &__prev-month {
    inset-inline-start: 30px;
  }

  &__next-month {
    inset-inline-end: 30px;
  }

  &__calendar {
    padding: 8px 12px;

    .#{$namespace}-calendar {
      &__row--week {
        margin-bottom: 6px;
      }
    }
  }

  @mixin calendar-panel {
    & {
      // here is header items style
      margin: 0 3px;
      cursor: pointer;
      transition: get-css-var('transition-color');

      &:hover {
        color: get-css-var('date-picker-header-color-hover');
      }
    }

    &-panel {
      display: grid;
      grid-template-rows: repeat(4, 1fr);
      grid-template-columns: repeat(3, 1fr);
      width: 224px;
      height: 230px;
    }

    &-label {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: 56px;
      height: 28px;
      background-color: transparent;
      border-radius: get-css-var('date-picker-item-radius');

      &::before {
        position: absolute;
        right: 0;
        left: 0;
        display: none;
        height: 24px;
        pointer-events: none;
        content: '';
        background-color: get-css-var('date-picker-range-bg-color');
        opacity: get-css-var('date-picker-range-opacity');
      }

      // ensure content above the range mask
      &-inner {
        position: relative;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
        background-color: get-css-var('date-picker-item-bg-color');
        border-radius: get-css-var('date-picker-item-radius');
        transition: get-css-var('transition-color'), get-css-var('transition-background');
      }
    }

    $label: '#{&}-label';
    $label-i: '#{&}-label-inner';

    &-item {
      position: relative;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;

      &--today {
        &,
        &:hover {
          #{$label-i} {
            color: get-css-var('date-picker-item-color-today');
            background-color: get-css-var('date-picker-item-bg-color-today');
          }
        }
      }

      &--prev,
      &--next {
        &,
        &:hover {
          #{$label-i} {
            color: get-css-var('date-picker-item-color-outside');
            background-color: get-css-var('date-picker-item-bg-color-outside');
          }
        }
      }

      &:hover #{$label-i} {
        color: get-css-var('date-picker-item-color-hover');
        background-color: get-css-var('date-picker-item-bg-color-hover');
      }

      &--selected {
        &,
        &:hover {
          #{$label-i} {
            color: get-css-var('date-picker-item-color-selected');
            background-color: get-css-var('date-picker-item-bg-color-selected');
          }
        }
      }

      &--disabled {
        &,
        &:hover {
          #{$label-i} {
            color: get-css-var('date-picker-item-color-disabled');
            cursor: not-allowed;
            background-color: get-css-var('date-picker-item-bg-color-disabled');
          }
        }
      }

      &--in-range #{$label} {
        &::before {
          display: block;
        }
      }
    }
  }

  &__year {
    @include calendar-panel;
  }

  &__month {
    @include calendar-panel;
  }

  &__time-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    border-inline-start: get-css-var('border-shape') get-css-var('date-picker-d-color');
  }

  &__wheel {
    display: flex;
    gap: 6px;
    height: calc(100% - #{get-css-var('date-picker-header-height')});
    padding: 0 10px;

    // .#{$namespace}-wheel__list {
    //   padding: 0 10px;
    // }

    .#{$namespace}-time-picker__wheel {
      display: flex;
      align-items: center;
      height: 100%;
    }
  }

  &__action {
    display: flex;
    justify-content: flex-end;
    width: 100%;
    padding: 6px 8px;
    border-top: get-css-var('border-shape') get-css-var('date-picker-d-color');
  }

  &--no-hour {
    .#{$wheel}:first-of-type {
      display: none;
    }
  }

  &--no-minute {
    .#{$wheel}:nth-of-type(2) {
      display: none;
    }
  }

  &--no-second {
    .#{$wheel}:last-of-type {
      display: none;
    }
  }
}
